<?php
class industry extends Zend_Db_Table{
	protected $_name='plm_industry';

	public function __construct(){
			parent::__construct();			
			$this->db=Zend_Registry::get('dbAdapter');
			$this->test=array();
	}

	public function getAll($id){				
		  $select=$this->db->select();
		  $select->from('plm_industry','*');
		  $select->where('industry_top_id = ?',$id);
		  $result=$this->db->fetchAll($select);	
		  foreach($result as $item){
			  $select=$this->db->select();
			  $select->from('plm_industry','*');
			  $select->where('industry_top_id = ?',$item['industry_id']);
			  $count=count($this->db->fetchAll($select));
			  if($count){
			  $data[]=array("id"		=> $item['industry_id'],
							"text"	=> $item['industry_name']
							  );
			  }
			  else{
			  $data[]=array("id"		=> $item['industry_id'],
							"text"	=> $item['industry_name'],
							"leaf"	=> true
							  );
				  }
			  }
		return json_encode($data);
		}
		
	public function test($id){
		$this->getOne($id);
		return json_encode($this->test);
		}
		
	public function getOne($id){
		  $select=$this->db->select();
		  $select->from('plm_industry','*');
		  $select->where('industry_top_id = ?',$id);
		  $result=$this->db->fetchAll($select);	
		   foreach($result as $item){
			  $select=$this->db->select();
			  $select->from('plm_industry','*');
			  $select->where('industry_top_id = ?',$item['industry_id']);
			  $count=count($this->db->fetchAll($select));			  
			  if($count){
			  $temp=array("id"		=> $item['industry_id'],
							"text"	=> $item['industry_name'],
						"children"	=> $this->getOne($item['industry_id'])
							  );
			  $this->test[]=$temp;
			  }
			  else{
			  $temp=array("id"				=> $item['industry_id'],
								  "text"	=> $item['industry_name'],
								  "leaf"	=> true
									);
			  $this->test[]=$temp;
			  return $temp;
			  }		
		   }
		}
		
}
?>